<?php


namespace app\models;


use app\Utils\LogUtil;
use yii\db\ActiveRecord;
use Swoole\Table;
class CronTask extends ActiveRecord
{

    const SWOOLE_TABLE_NUM = 1024;//swoole_table加载的最大容量,单位:字节，内存不足将直接创建失败
    public static $swoole_table;
    public static $column = [
        'id' => [Table::TYPE_INT, 8],
        'is_remote' => [Table::TYPE_INT, 8],
        'shell_content' => [Table::TYPE_STRING, 200],
        'start_time' => [Table::TYPE_STRING, 200],
        'status' => [Table::TYPE_INT, 8],
    ];
    public function init()
    {
        parent::init();
        self::$swoole_table = new Table(self::SWOOLE_TABLE_NUM * 20);

        foreach (self::$column as $key => $v) {

            self::$swoole_table->column($key, $v[0], $v[1]);
        }
        self::$swoole_table->create();
        $this->register();
    }

    //注册定时任务
    public  function register()
    {
        //读取db的任务数据加载到，暂时读取固定的数组
        $temp_arr = [

            [
                'id' => 1,
                'is_remote' => 2,
                'shell_content' => 'http://ajax.588ku.com/?m=Test&a=index',
                'start_time' => '* 20 01 * * *',
                'status' => 0
            ],
            [
                'id' => 2,
                'is_remote' => 2,
                'shell_content' => 'http://ajax.588ku.com/?m=Test&a=index',
                'start_time' => '* 20 01 * * *',
                'status' => 0
            ]
        ];


        foreach ($temp_arr as $item)
        {

            if (self::$swoole_table->count() > self::SWOOLE_TABLE_NUM) {

                LogUtil::log('警告：进程任务数量不能超过'.self::SWOOLE_TABLE_NUM);
                return true;
            }

            self::$swoole_table->set($item['id'],$item);
        }
    }
    //清理所有定时任务
    public function clearTask()
    {


    }
    //记录任务执行日志
    public function taskRunLog()
    {



    }
    //获取任务执行列表
    public function getTask()
    {
        return self::$swoole_table;
    }

}